'''
Author: https://gitee.com/timecontract
Date: 2022-07-20 11:05:17
LastEditors: https://gitee.com/timecontract
LastEditTime: 2022-07-22 11:34:53
FilePath: \python_analysis_extra\packet.py
Description: 

'''

import hashlib
import os
import json
import zipfile

folder = 'python_analysis_extra/'
zip_name = 'python_analysis_extra.zip'
out_file_name = 'packet.json'


def run_cmd_Popen_fileno(cmd: str):
    '''
    执行cmd命令, 并得到执行后的返回值, python调试界面输出返回值
    :param cmd_string: cmd命令, 如: 'adb devices'
    :return:
    '''
    import subprocess
    print('subprocess.Popen>{}'.format(cmd))
    return subprocess.Popen(cmd, shell=True, stdout=None, stderr=None).wait()


def git_update(info: str):
    run_cmd_Popen_fileno('git add .')
    run_cmd_Popen_fileno('git commit -m '+info)
    run_cmd_Popen_fileno('git push origin master')


if __name__ == '__main__':
    out_file = folder+out_file_name
    zip = zipfile.ZipFile(zip_name, 'w', zipfile.ZIP_DEFLATED)
    with open(out_file, 'w')as out_fp:
        dic = {}
        for file_name in os.listdir(folder):
            file_path = folder+file_name
            if os.path.isfile(file_path) and file_name != out_file_name:
                file_info = os.stat(file_path)
                file_size = file_info.st_size
                with open(file_path, encoding='utf-8')as fp:
                    data = fp.read()
                file_md5 = hashlib.md5(data.encode('utf-8')).hexdigest()
                dic[file_name] = ','.join((str(file_size), file_md5))
                zip.write(file_path, file_name)
        out_fp.write(json.dumps(dic))
    zip.write(out_file, out_file_name)
    zip.close()
    git_update('info')
